package com.wiz.web.manager;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.wiz.common.Constant;
import com.wiz.common.util.DateUtil;
import com.wiz.web.dao.mapper.MemberMapper;
import com.wiz.web.vo.WxModelMessageVO;
import com.wiz.web.vo.criteria.MemberCriteria;
import com.wiz.web.vo.to.AccountChargeHistoryTO;
import com.wiz.web.vo.to.MemberTO;

/**
 * 微信接口调用
 * @author Hakeny
 */
@Service("wxManager")
public class WxManager implements Constant{
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private HttpManager httpManager;
	@Resource
	private MemberMapper memberMapper;
	
	/**
	 * 交易消息
	 * @param vo
	 * @param memberId
	 * @throws Exception
	 */
	public void consumpMessage(WxModelMessageVO vo,String memberId){
		MemberCriteria criteria = new MemberCriteria();
		criteria.setMemberId(memberId);
		criteria.setHqId(vo.getHqId());
		MemberTO memberTO = memberMapper.getMemberInfo(criteria);
		if(memberTO==null){
			return;
		}
		if(StringUtils.isBlank(memberTO.getOpenId())){
			return;
		}
		List<NameValuePair> formparams=new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("openId", memberTO.getOpenId()));
		formparams.add(new BasicNameValuePair("hqName", vo.getHqName()));
		formparams.add(new BasicNameValuePair("stationName", vo.getStationName()));
		formparams.add(new BasicNameValuePair("amount", vo.getAmount()));
		formparams.add(new BasicNameValuePair("point", vo.getPoint()));
		formparams.add(new BasicNameValuePair("balanceAmount", vo.getBalanceAmount()));
		formparams.add(new BasicNameValuePair("balancePoint", vo.getBalancePoint()));
		formparams.add(new BasicNameValuePair("paidTypeName", vo.getPaidTypeName()));
		formparams.add(new BasicNameValuePair("createdTime", vo.getCreatedTime()));
		try {
			httpManager.getHttpResponseByPost(WX_IP, WX_CONSUMP, formparams);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 充值消息
	 * @param to
	 * @throws Exception
	 */
	public void chargeMessage(AccountChargeHistoryTO to){
		MemberCriteria criteria = new MemberCriteria();
		criteria.setMemberId(to.getMemberId());
		criteria.setHqId(to.getHqId());
		MemberTO memberTO = memberMapper.getMemberInfo(criteria);
		if(memberTO==null){
			return;
		}
		if(StringUtils.isBlank(memberTO.getOpenId())){
			return;
		}
		List<NameValuePair> formparams=new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("openId", memberTO.getOpenId()));
		formparams.add(new BasicNameValuePair("hqName", to.getHqName()));
		formparams.add(new BasicNameValuePair("stationName", to.getStationName()));
		formparams.add(new BasicNameValuePair("amount", String.valueOf(to.getAmount())));
		formparams.add(new BasicNameValuePair("giftAmount", String.valueOf(to.getGiftAmount())));
		formparams.add(new BasicNameValuePair("balanceAmount", String.valueOf(memberTO.getAmount())));
		formparams.add(new BasicNameValuePair("createdTime", to.getCreatedTime()));
		try {
			httpManager.getHttpResponseByPost(WX_IP, WX_CHARGE, formparams);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 积分兑换消息
	 * @param vo
	 * @param memberId
	 */
	public void exchangePointMessage(WxModelMessageVO vo,String memberId){
		MemberCriteria criteria = new MemberCriteria();
		criteria.setMemberId(memberId);
		criteria.setHqId(vo.getHqId());
		MemberTO memberTO = memberMapper.getMemberInfo(criteria);
		if(memberTO==null){
			return;
		}
		if(StringUtils.isBlank(memberTO.getOpenId())){
			return;
		}
		List<NameValuePair> formparams=new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("openId", memberTO.getOpenId()));
		formparams.add(new BasicNameValuePair("hqName", vo.getHqName()));
		formparams.add(new BasicNameValuePair("stationName", vo.getStationName()));
		formparams.add(new BasicNameValuePair("point", vo.getPoint()));
		formparams.add(new BasicNameValuePair("balancePoint", vo.getBalancePoint()));
		formparams.add(new BasicNameValuePair("createdTime", DateUtil.formatDateTime(new Date())));
		try {
			httpManager.getHttpResponseByPost(WX_IP, WX_EXCHANGE_POINT, formparams);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
